// pages/xiaodangan/xiaodangan.js
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [[2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1], ['至'], [2018], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1]],
    multiIndex: [0, 0, 0, 0, 0, 0, 0],
    multiIndexqd: [0, 0, 0, 0, 0, 0, 0],
    data:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //加载日期选择器
    var date = new Date();
    var year = parseInt(date.getFullYear());
    var month = date.getMonth() + 1;
    var date = date.getDate();

    var dates = [];
    if (month % 2 == 0 && month != 2) {
      for (var i = 1; i <= 31; i++) {
        dates.push(i);
      }
    } else if (month % 2 == 1) {
      for (var i = 1; i <= 30; i++) {
        dates.push(i);
      }
    } else {
      if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
        for (var i = 1; i <= 29; i++) {
          dates.push(i);
        }
      } else {
        for (var i = 1; i <= 28; i++) {
          dates.push(i);
        }
      }
    }

    var years = [];
    for (var i = year; i >= year - 10; i--) {
      years.push(i);
    }
    this.data.multiArray[0] = years;
    this.data.multiArray[2] = dates;
    this.data.multiArray[4] = years;
    this.data.multiArray[6] = dates;
    this.setData({
      multiArray: this.data.multiArray,
      multiIndex: [0, month - 1, date-1, 0, 0, month - 1, date-1],
      multiIndexqd: [0, month - 1, date-1, 0, 0, month - 1, date-1]
    });
  },
  bindMultiPickerColumnChange: function (e) {
    if (e.detail.column == 0 || e.detail.column == 4) {
      var year = this.data.multiArray[e.detail.column][e.detail.value];
      var month = this.data.multiIndex[e.detail.column + 1] + 1;
      var dates = [];
      if (month == 2) {
        if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
          for (var i = 1; i <= 29; i++) {
            dates.push(i);
          }
        } else {
          for (var i = 1; i <= 28; i++) {
            dates.push(i);
          }
        }
      } else if (month % 2 == 0) {
        for (var i = 1; i <= 30; i++) {
          dates.push(i);
        }
      } else {
        for (var i = 1; i <= 31; i++) {
          dates.push(i);
        }
      }
      this.data.multiArray[e.detail.column + 2] = dates;
    } else if (e.detail.column == 1 || e.detail.column == 5) {
      var year = this.data.multiIndex[e.detail.column - 1];
      var month = e.detail.value + 1;
      var dates = [];
      if (month == 2) {
        if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0)) {
          for (var i = 1; i <= 29; i++) {
            dates.push(i);
          }
        } else {
          for (var i = 1; i <= 28; i++) {
            dates.push(i);
          }
        }
      } else if (month % 2 == 0) {
        for (var i = 1; i <= 30; i++) {
          dates.push(i);
        }
      } else {
        for (var i = 1; i <= 31; i++) {
          dates.push(i);
        }
      }
      this.data.multiArray[e.detail.column + 1] = dates;
    }
    this.data.multiIndex[e.detail.column] = e.detail.value;
    this.setData({
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    });
  },
  bindMultiPickerChange: function (e) {
    this.setData({
      multiIndex: e.detail.value,
      multiIndexqd: e.detail.value
    });
    console.log(e.detail.value);
    var date_min = this.data.multiArray[0][e.detail.value[0]] + '-' + njzxw.zeroize(this.data.multiArray[1][e.detail.value[1]]) + '-' + njzxw.zeroize(this.data.multiArray[2][e.detail.value[2]]);
    var date_max = this.data.multiArray[4][e.detail.value[4]] + '-' + njzxw.zeroize(this.data.multiArray[5][e.detail.value[5]]) + '-' + njzxw.zeroize(this.data.multiArray[6][e.detail.value[6]]);

    this.loadData(date_min,date_max);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var $this = this;
    
    var date = njzxw.format(new Date(), 'yyyy-MM-dd');
    this.loadData(date, date);
  },
  loadData: function (oper_date_min,oper_date_max){
    var $this = this;
    njzxw.ajax({
      url: url.userArchivesInfo.queryPage,
      load:true,
      data:{
        oper_date_min: oper_date_min,
        oper_date_max: oper_date_max,
      },
      fun: function (a, b) {
        $this.setData({
          data: b.data.data
        });
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})